package xyz.lwm.lazycat.web.route;

import xyz.lwm.lazycat.utility.StringUtil;
import xyz.lwm.lazycat.utility.scan.PathRegex;
import xyz.lwm.lazycat.web.handle.Handler;
import xyz.lwm.lazycat.web.handle.HandlerType;

import static xyz.lwm.lazycat.utility.StrPool.STR_ASTERISK;

/**
 * 简单路由
 *
 * @author lwm
 */
class SimpleRoute extends AbstractRoute {

    /**
     * 路径正则
     */
    private final PathRegex regex;

    public SimpleRoute(String path, HandlerType type, Handler handler) {
        super(path, type, handler);
        this.regex = PathRegex.toRegex(path);
    }

    @Override
    public String getKey() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean matches(String path) {
        if (StringUtil.isBlank(path)) {
            return false;
        }
        if (STR_ASTERISK.equals(getPath())) {
            return true;
        }
        return regex.matches(path);
    }

}
